package luke4fun.wheresmymoney.adapter.list.impl;

import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.bean.BudgetBean;
import luke4fun.wheresmymoney.bean.BudgetDetailBean;
import luke4fun.wheresmymoney.util.FormatUtil;

public class BudgetInfoListAdapter extends BaseExpandableListAdapter {

	private List<BudgetBean> dataList = new ArrayList<BudgetBean>(0);
	private Context context;
	private LayoutInflater layoutInflater;
	
	public BudgetInfoListAdapter(Context context) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return dataList.get(groupPosition).getDetails().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return dataList.get(groupPosition).getDetails().get(childPosition).getId();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		
		ChildViewHolder holder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.list_budget_detail, null);
			holder = new ChildViewHolder();
			holder.category = (TextView) convertView.findViewById(R.id.category);
			holder.budgetExp = (TextView) convertView.findViewById(R.id.budget_exp);
			holder.percentBar = (View) convertView.findViewById(R.id.percent_bar);
			holder.emptyBar = (View) convertView.findViewById(R.id.empty_bar);
			
			convertView.setTag(holder);
		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}
		
		BudgetDetailBean bean = dataList.get(groupPosition).getDetails().get(childPosition);
		holder.category.setText(bean.getCategory());
		holder.budgetExp.setText(bean.getExpense() + " / " + bean.getBudget() + "");
		holder.percentBar.setBackgroundColor(bean.getCategoryColor());
		holder.emptyBar  .setBackgroundColor(bean.getCategoryColor());
		holder.emptyBar  .setAlpha(0.5f);
		
		float percent = 100 * bean.getExpense() / bean.getBudget();
		int width = Math.round(parent.getMeasuredWidth() * percent / 100);
		
		// animation
		ValueAnimator ani = ValueAnimator.ofObject(new WidthEvaluator(holder.percentBar), 0, width);
		ani.setDuration(800L);
		ani.setInterpolator(new AccelerateDecelerateInterpolator());
		ani.start();
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return dataList.get(groupPosition).getDetails().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return dataList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return dataList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return dataList.get(groupPosition).getId();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupViewHolder holder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.list_budgets, null);
			holder = new GroupViewHolder();
			holder.budgetDateFrom = (TextView) convertView.findViewById(R.id.budget_date_from);
			holder.budgetDateTo   = (TextView) convertView.findViewById(R.id.budget_date_to);
			holder.budget = (TextView) convertView.findViewById(R.id.total_budget);
			
			convertView.setTag(holder);
		} else {
			holder = (GroupViewHolder) convertView.getTag();
		}
		
		BudgetBean bean = dataList.get(groupPosition);
		holder.budgetDateFrom.setText(FormatUtil.formatDateToMMddyyyy( new Date(bean.getFrom()) ));
		holder.budgetDateTo.setText(FormatUtil.formatDateToMMddyyyy( new Date(bean.getTo()) ));
		holder.budget.setText(bean.getBudget() + "");
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	public void updateList(List<BudgetBean> list) {
		dataList.clear();
		dataList.addAll(list);
		notifyDataSetChanged();
	}
	
	class GroupViewHolder {
		public TextView budgetDateFrom;
		public TextView budgetDateTo;
		public TextView budget;
	}
	
	class ChildViewHolder {
		public TextView category;
		public TextView budgetExp;
		public View percentBar;
		public View emptyBar;
	}
	
	/**
	 * widthEvaluator
	 * @author chihzhe
	 *
	 */
	private class WidthEvaluator extends IntEvaluator {

	    private View v;
	    
	    public WidthEvaluator(View v) {
	        this.v = v;
	    }

	    @Override
	    public Integer evaluate (float fraction, Integer startValue, Integer endValue) {
	        int num = super.evaluate(fraction, startValue, endValue);
	        
	        ViewGroup.LayoutParams params = v.getLayoutParams();
	        params.width = num;
	        v.setLayoutParams(params);
	        
	        return num;
	    }
	}

}
